package com.example.myapplication.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.myapplication.R;
import com.example.myapplication.activity.general.WebViewActivity;
import com.example.myapplication.activity.home.SearchActivity;
import com.example.myapplication.bean.BannerBean;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Ng
 * @Date: 2024/4/7
 * @Versopn: 1.0.0
 */
public class HomeFragment extends BaseFragment {

    private static final String TAG = HomeFragment.class.getSimpleName();
    private Banner banner;
    private EditText editText;
    private List<BannerBean.RowsDTO> list;

    @Override
    public View initView() {
        Log.i(TAG, "首页的视图被实例化了..");
        View view = View.inflate(getActivity(), R.layout.fragment_home, null);
        banner = (Banner) view.findViewById(R.id.fragment_home_banner);
        editText = (EditText) view.findViewById(R.id.fragment_home_search);
        return view;
    }

    @Override
    public void initData() {
        super.initData();

        initSearch();
        initBanner();
    }

    /**
     * 初始化搜索
     */
    private void initSearch() {
        editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                if (i == EditorInfo.IME_ACTION_SEARCH) {
                    String search = editText.getText().toString();
                    Intent intent = new Intent(getActivity(), SearchActivity.class);
                    intent.putExtra("search", search);
                    startActivity(intent);
                    return true;
                }
                return false;
            }
        });
    }

    /**
     * 初始化轮播图
     */
    private void initBanner() {
        // 网络加载图片
        list = new ArrayList<>();
        list.add(new BannerBean.RowsDTO("http://pic.gooooal.com/images/102422/102422663.jpg", "111"));
        list.add(new BannerBean.RowsDTO("http://pic.gooooal.com/images/102422/102422663.jpg", "222"));

        banner.setAdapter(new BannerImageAdapter<BannerBean.RowsDTO>(list) {
            @Override
            public void onBindView(BannerImageHolder holder, BannerBean.RowsDTO data, int position, int size) {
                // BannerImageHolder 利用 Banner 实现图片加载，也可自己实现图片加载
                Glide.with(getActivity())
                        .load(data.getImgUrl())
                        .apply(RequestOptions.bitmapTransform(new RoundedCorners(30)))
                        .into(holder.imageView);
            }
        }).addBannerLifecycleObserver(this)  // 添加声明周期观察者
                .setIndicator(new CircleIndicator(getActivity()))
                .setOnBannerListener(new OnBannerListener() {
                    @Override
                    public void OnBannerClick(Object data, int position) {
                        Intent intent = new Intent(getActivity(), WebViewActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putString("url", list.get(position).getImgUrl());
                        intent.putExtras(bundle);
                        getActivity().startActivity(intent);
                    }
                });
    }
}
